package com.shr25.robot.qq.plugins;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.shr25.robot.qq.model.QqMessage;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MusicKind;
import net.mamoe.mirai.message.data.MusicShare;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 音乐分享
 *
 * @author huobing
 * @date 2022-6-24 14:14
 */
@Component
public class MusicSharePlugin extends RobotPlugin {
  public static final String api = "http://pd.musicapp.migu.cn/MIGUM3.0/v1.0/content/search_all.do?&ua=Android_migu&version=5.0.1&text=SONGNAME&pageNo=1&pageSize=1&searchSwitch={%22song%22:1,%22album%22:0,%22singer%22:0,%22tagSong%22:0,%22mvSong%22:0,%22songlist%22:0,%22bestShow%22:1}";

  public static final String musicUrl = "http://app.pd.nf.migu.cn/MIGUM2.0/v1.0/content/sub/listenSong.do?toneFlag={formatType}&netType=00&userId=15548614588710179085069&ua=Android_migu&version=5.1&copyrightId=0&contentId={contentId}&resourceType={resourceType}&channel=0";

  public MusicSharePlugin() {
    super();
    setName("咪咕音乐分享插件");
    addDesc("从咪咕音乐搜索，咪咕音乐可以免费听周杰伦的所有歌曲");
    addCommand("#咪咕 {歌曲名称}", "从咪咕音乐搜索", true);
    setSort(1002);
  }

  @Override
  public boolean executeGroupMessage(QqMessage qqMessage) {
    String command = qqMessage.getCommand();
    if(command != null){
      String name = qqMessage.getParameter("#"+command);
      if(name != null){
        switch (command){
          case "咪咕":
            qqMessage.putReplyMessage(miguMusic(name));
            break;
        }
      }
    }
    return true;
  }

  private MessageChain miguMusic(String name) {
    String url = null;
    try {
      url = api.replace("SONGNAME", URLEncoder.encode(name,"UTF-8"));

      HttpResponse httpResponse = HttpRequest.get(url).execute();

      JSONObject data = JSONUtil.parseObj(httpResponse.body());

      Map<String, Object> m0 = (Map<String, Object>) data.get("songResultData");
      Map<String, Object> m1 = (Map<String, Object>) ((List)m0.get("result")).get(0);
      String copyrightId = (String) m1.get("copyrightId");
      String resourceType = (String) m1.get("resourceType");
      String contentId = (String) m1.get("contentId");
      String songName = (String) m1.get("name");
      String singer = (String) ((Map)((List)m1.get("singers")).get(0)).get("name");
      String picUrl = (String)m1.get("lyricUrl");
      String u0 = musicUrl.replace("{formatType}", "HQ")
        .replace("{contentId}", contentId)
        .replace("{resourceType}", resourceType);
      MusicShare music = new MusicShare(
        MusicKind.MiguMusic,
        songName,
        singer  + "(请点击右侧按钮直接播放)",
        "http://music.migu.cn/v3/music/song/" + copyrightId,
        "https://img0.baidu.com/it/u=2767805314,105989167&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
        u0
      );
      return buildMessageChain(music);
    } catch (UnsupportedEncodingException e) {
      return buildMessageChain("查无此歌");
    }
  }
}
